
**MAKING VARIABLES 

//converting dates to usable form 
format type_daten %td

//sample population generation >> pts w/ referral after implementation of navigators
	//navigator start date used 1/5/2017
		//appropriate time period 
		generate estrellas=(type_daten>td(30apr2017))
	//population that was received at least 1 referral during the estrellas period 
generate referred = 0
replace referred =1 if estrellas==1 & (typen==1 | typen==2 | typen==3) 
	//success of referral (emergency and routine included)
generate genreferralsuc = 0
replace genreferralsuc = 0 if dispon==7
replace genreferralsuc = 1 if dispon == 1|dispon == 2|dispon == 3|dispon == 4|dispon == 5|dispon == 6| dispon == 8|dispon == 9|dispon == 10
label define genreferralsuc_lbl 0"unsuccessful" 1"successful"
label values genreferralsuc genreferralsuc_lbl
	//population that received emergency referral 
gen emergref = .
replace emergref=0 if typen==1 & dispon==7 
replace emergref=1 if typen==1 & dispon !=7
label define emergref_lbl 0"unsuccessful emergency referral" 1"successful emergency ref"
label values emergref emergref_lbl
	//population that received a routine referral 
gen routineref = .
replace routineref = 0 if (typen==2 | typen==3) & dispon==7
replace routineref = 1 if (typen==2 | typen==3) & dispon !=7
label define routineref_lbl 0"unsuccessful routine ref" 1"successful routine ref"
label values routineref routineref_lbl


//redefining variables for tabulations 
	//excluding extremees of ages (<10, >60)
replace age=. if age<10 | age>60
	
	**variables about complications**
	//making variables for the number of past pregnancy complications (max # of complications was 4)

recode pastcomp1n (10=0)(1/9=1) (11/20=1), gen(pastcomp1n1)
recode pastcomp2n (9=0)(1/8=1)(10/21=1), gen(pastcomp2n2)
recode pastcomp3n (9=0)(1/8=1)(10/18=1), gen(pastcomp3n3)
recode pastcomp4n (6=0)(1/5=1)(7/12=1),gen(pastcomp4n4)
	//making variable for total number of past pregnancy complications 
gen totpastcomp = 0
replace totpastcomp=. if totpastcomp==0
replace totpastcomp=0 if pastcomp1n1==0 
replace totpastcomp = 1 if pastcomp1n1 ==1
replace totpastcomp = 2 if pastcomp2n2 ==1
replace totpastcomp = 3 if pastcomp3n3==1
label define totpastcomp_lbl 0"0 complications" 1"1 complication" 2"2 complications" 3"3 complications" 4"4complications"
label values totpastcomp totpastcomp_lbl
	//making dummy variable for any past comp
gen anypastcomp = 0
replace anypastcomp=. if anypastcomp==0
replace anypastcomp=0 if totpastcomp==0
replace anypastcomp=1 if totpastcomp == 1 | totpastcomp ==2 | totpastcomp==3
label define anypastcomp_lbl 0"no past comp" 1"at least 1 past comp"
label values anypastcomp anypastcomp_lbl
	//making variables for # of past neonatal complications 
recode neocomp1n (7=0) (1/6=1) (8/10=1), gen (neocomp1n1)
recode neocomp2n (3=0) (1/2=1) (4/6=1), gen (neocomp2n2)
recode neocomp3n (1=1), gen (neocomp3n3)
	//making variable for total number of past neonatal complications 
gen totneocomp = 0
replace totneocomp=. if totneocomp==0
replace totneocomp=0 if neocomp1n1==0
replace totneocomp = 1 if neocomp1n1 ==1
replace totneocomp = 2 if neocomp2n2 ==1
replace totneocomp = 3 if neocomp3n3==1
	//making dummy variables for any past neonatal complication 
gen anyneocomp =.
replace anyneocomp=0 if totneocomp==0
replace anyneocomp=1 if totneocomp == 1 | totneocomp ==2 | totneocomp==3
label define anyneocomp_lbl 0"no neo comp" 1"at least 1 neo comp"
label values anyneocomp anyneocomp_lbl
	//making variables for # of current pregnancy complications 
recode currentcomp1n (3=0)(1/2=1)(4/7=1), gen (currentcomp1n1)
recode currentcomp2n (3=0)(1/2=1)(4/6=1), gen (currentcomp2n2)
recode currentcomp3n (1/4=1), gen (currentcomp3n3)
recode currentcomp4n (1=1), gen (currentcomp4n4)
	//making variable for total number current pregnancy complications 
gen totcurrentcomp =.
replace totcurrentcomp=0 if currentcomp1n1==0
replace totcurrentcomp = 1 if currentcomp1n1 ==1
replace totcurrentcomp = 2 if currentcomp2n2 ==1
replace totcurrentcomp = 3 if currentcomp3n3 ==1
replace totcurrentcomp = 4 if currentcomp4n4 ==1
	//making dummy variable for any current preg comp 
gen anycurrentcomp =.
replace anycurrentcomp=0 if totcurrentcomp==0
replace anycurrentcomp=1 if totcurrentcomp == 1 | totcurrentcomp ==2 | totcurrentcomp==3 | totcurrentcomp==4
label define anycurrentcomp_lbl 0"no current comp" 1"at least 1 current comp"
label values anycurrentcomp anycurrentcomp_lbl
	//variable for any past complications (neonatal or past maternal)
gen pastcomp =.
replace pastcomp=0 if anypastcomp==0 | anyneocomp==0 
replace pastcomp=1 if anypastcomp ==1 | anyneocomp==1
label define pastcomp_lbl 0"no past neo/mat comp" 1"at least 1 past neo/mat comp"
label values pastcomp pastcomp_lbl

	//variable about skilled vs unskilled ANC - unskilled = midwife and below; skilled== puesto and above 
gen ancskill=.
replace ancskill=0 if anclocn==1| anclocn== 4| anclocn==5
replace ancskill=1 if anclocn==2| anclocn==3| anclocn==6| anclocn==7
label define ancskill_lbl 0"unskilled ANC" 1"skilled ANC" 
label values ancskill ancskill_lbl
	//type of referral (ante/intrapartum vs post/neonatal)
gen reftype=.
replace reftype=0 if emergtypen==1| emergtypen==2
replace reftype=1 if emergtypen==3 | emergtypen==4
label define reftype_lbl 0"ante/intra" 1"post/neo"
label value reftype reftype_lbl
	//variable for primigravid vs not 
recode gravid (1=1)(2/17=0), gen (primigravid) 
label define primigravid_lbl 0"not primigravid" 1"primigravid"
label values primigravid primigravid_lbl
	//redefining "willingness" answers so they're in numerical order
recode willingnessn (1=2)(2=4)(3=3)(4=1)(5=5), gen (willingness1)
label define willingness1_lbl 1"strongly agree" 2"agree" 3"indifferent" 4"disagree" 5"strongly disagree"
label values willingness1 willingness1_lbl
	//redefining "willingness" into two categories 
recode willingnessn (1=0)(2=1)(3=1)(4=0)(5=1), gen (will_dummy)
label define will_dummy_lbl 0"agree-strongly agree" 1"indifferent-strongly disagree"
label values will_dummy will_dummy_lbl


*TABLE 1
	*Tabulations for emergency referrals 
	
	*chi2 included for categorical variables 

tab age emergref if referred==1

tabstat age if referred==1, statistics( median iqr p25 p75 ) by(emergref )

tab primigravid emergref if referred ==1, column chi2 

tab ancskill emergref  if referred ==1, column chi2 

tab reftype emergref  if referred ==1, column chi2

tab anycurrentcomp emergref if referred==1, column chi2

tab pastcomp emergref if referred==1, column chi2

tab will_dummy emergref if referred ==1, column chi2


	*checking normality of continuous variables 
	by emergref, sort:stem age if referred==1
	by emergref, sort:stem gravid if referred==1


	*hypothesis testing for continuous variables
	ranksum age if referred==1, by(emergref )
	ranksum gravid if referred==1, by(emergref )

*Tabulations for routine referrals 
	*chi2 included for categorical variables 

tab age routineref if referred==1

tabstat age if referred==1, statistics( median iqr p25 p75 ) by(routineref)

tab primigravid routineref if referred ==1, column chi2  

tab ancskill routineref  if referred ==1, column chi2 

tab reftype routineref  if referred ==1, column chi2  

tab anycurrentcomp routineref if referred==1, column chi2 

tab pastcomp routineref if referred==1, column chi2  

tab will_dummy routineref if referred ==1, column chi2 


	*checking normality of continuous variables 
	by routineref, sort:stem age if referred==1
	by routineref, sort:stem gravid if referred==1


	*hypothesis testing for continuous variables
	ranksum age if referred==1, by(routineref)
	ranksum gravid if referred==1, by(routineref)

*TABLE 2

//EMERGENCY REFERRALS
	firthlogit emergref i.ancskill if referred==1, or 
	firthlogit emergref i.reftype if referred==1, or
	firthlogit emergref i.primigravid if referred==1, or
	firthlogit emergref i.anycurrentcomp if referred==1, or
	firthlogit emergref i.pastcomp if referred==1, or 
	firthlogit emergref i.will_dummy if referred==1, or
	firthlogit emergref age if referred==1, or	
		
//ROUTINE REFERRALS

	logistic routineref i.ancskill if referred==1
	logistic routineref i.reftype if referred==1
	logistic routineref i.primigravid if referred==1
	logistic routineref i.anycurrentcomp if referred==1	
	logistic routineref i.pastcomp if referred==1
	logistic routineref i.will_dummy if referred==1
	logistic routineref age if referred==1	


*TABLE 3

*EMERGENCY REFERRALS 
*maximum model 
	*for emergency referrals, "or" added as an option at end of code to get OR instead of coeff

firthlogit emergref age anycurrentcomp pastcomp will_dummy ancskill reftype primigravid if referred== 1, or

est store m1

firthlogit emergref age anycurrentcomp pastcomp will_dummy if e(sample) & referred==1, or

est store m2

lrtest m1 m2

*clinical variables do not add explanatory power

*removing current complications 

firthlogit emergref age anycurrentcomp pastcomp will_dummy if referred==1, or
firthlogit emergref age pastcomp will_dummy if referred==1, or

quietly firthlogit emergref age anycurrentcomp pastcomp will_dummy if referred==1, or

est store m1

quietly firthlogit emergref age pastcomp will_dummy if e(sample) & referred==1, or

est store m2

lrtest m1 m2

*current complications does not add value 

*removing past complications 

firthlogit emergref age anycurrentcomp pastcomp will_dummy if referred==1, or
firthlogit emergref age anycurrentcomp will_dummy if referred==1, or

quietly firthlogit emergref age anycurrentcomp pastcomp will_dummy if referred==1, or

est store m1

quietly firthlogit emergref age anycurrentcomp will_dummy if e(sample) & referred==1, or

est store m2

lrtest m1 m2

*past complications does not add to model

*removing past and current complications in one block 

firthlogit emergref age anycurrentcomp pastcomp will_dummy if referred==1, or
firthlogit emergref age will_dummy if referred==1, or

quietly firthlogit emergref age anycurrentcomp pastcomp will_dummy if referred==1, or

est store m1

quietly firthlogit emergref age will_dummy if e(sample) & referred==1, or

est store m2

lrtest m1 m2

*removing will_dummy 

firthlogit emergref age anycurrentcomp pastcomp will_dummy if referred==1, or
firthlogit emergref age anycurrentcomp pastcomp if referred==1, or

quietly firthlogit emergref age anycurrentcomp pastcomp will_dummy if referred==1, or

est store m1

quietly firthlogit emergref age anycurrentcomp pastcomp if e(sample) & referred==1, or

est store m2

lrtest m1 m2

*willingness does not add explanatory power


*ROUTINE REFERRALS 
*maximum model 

logistic routineref age anycurrentcomp pastcomp will_dummy ancskill reftype primigravid if referred== 1

*Hosmer Lemeshow GoF - grouping by 10 bc without this groups the number covariate patterns is close
//to the number of observations
estat gof, group (10)

est store m1

*removing clinical variables 
logistic routineref age anycurrentcomp pastcomp will_dummy if e(sample) & referred==1

est store m2

lrtest m1 m2

*clinical variables do not add explanatory power

*removing current complications 

logistic routineref age anycurrentcomp pastcomp will_dummy if referred==1

estat gof, group(10)

logistic routineref age pastcomp will_dummy if referred==1

estat gof, group(10)

quietly logistic routineref age anycurrentcomp pastcomp will_dummy if referred==1

est store m1

quietly logistic routineref age pastcomp will_dummy if e(sample) & referred==1

est store m2

lrtest m1 m2

*current complications add value

*removing past complications 

logistic routineref age anycurrentcomp pastcomp will_dummy if referred==1
logistic routineref age anycurrentcomp will_dummy if referred==1

estat gof, group(10)

quietly logistic routineref age anycurrentcomp pastcomp will_dummy if referred==1

est store m1

quietly logistic routineref age anycurrentcomp will_dummy if e(sample) & referred==1

est store m2

lrtest m1 m2

*past complications add value 

*removing past and current complications in one block to verify that yes, they add explanatory power

logistic routineref age anycurrentcomp pastcomp will_dummy if referred==1
logistic routineref age will_dummy if referred==1

estat gof, group(10)

quietly logistic routineref age anycurrentcomp pastcomp will_dummy if referred==1

est store m1

quietly logistic routineref age will_dummy if e(sample) & referred==1

est store m2

lrtest m1 m2

*removing will_dummy 

logistic routineref age anycurrentcomp pastcomp will_dummy if referred==1
logistic routineref age anycurrentcomp pastcomp if referred==1

estat gof, group(10)

quietly logistic routineref age anycurrentcomp pastcomp will_dummy if referred==1

est store m1

quietly logistic routineref age anycurrentcomp pastcomp if e(sample) & referred==1

est store m2

lrtest m1 m2

*willingness also adds value 

*clinically significant model 

logistic routineref age anycurrentcomp pastcomp will_dummy if referred==1

*do coefficients make sense? yes..no past complications, less likely to seek medical care
// current pregnancy complications more likely to seek facility level care
// stated low motivation to go to hosp in prenatal visit associated with unsuccessful referral





